<?php

class CityController extends GxController {

    public function filters() {
        return array('rights');
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'City'),
        ));
    }

    public function actionCreate() {
        $model = new City;

        $this->performAjaxValidation($model, 'city-form');

        if (isset($_POST['City'])) {
            $model->setAttributes($_POST['City']);

            if ($model->save()) {
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'City');

        $this->performAjaxValidation($model, 'city-form');

        if (isset($_POST['City'])) {
            $model->setAttributes($_POST['City']);

            if ($model->save()) {
                $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'City')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('City');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $model = new City('search');
        $model->unsetAttributes();
        $statuschangeurl = Yii::app()->createUrl('city/statuschange/');
        if (isset($_GET['City']))
            $model->setAttributes($_GET['City']);

        $this->render('admin', array(
            'model' => $model,
            'statuschangeurl' => $statuschangeurl,
        ));
    }

    public function actionStatuschange() {

        $model = $this->loadModel($_POST['id'], 'City');
        $model->status = $_POST['status'];
        $model->save();
    }

    public function actionDynamicState() {
        $data = State::model()->findAll('country_id=:country_id AND is_active=:is_active', array(':country_id' => (int) $_POST['City']['country_id'], 'is_active' => '1'));
        $data = CHtml::listData($data, 'id', 'name');
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

}